//
//  BIDDealInfoViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/12.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDDealInfoViewController.h"
#import "BIDFunctionSubCell.h"
#import "BIDDisclaimerView.h"

@interface BIDDealInfoViewController ()<BIDDisclaimerViewDelegate>
{
    NSArray *_dataSourceArr;
    BIDDisclaimerView *_disclaimerView;
    UIView *_maskView;
}
@end

@implementation BIDDealInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"交易信息";
    [_collectionView registerClass:[BIDFunctionSubCell class] forCellWithReuseIdentifier:@"identifier"];
    //
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    _disclaimerView = [[[NSBundle mainBundle] loadNibNamed:@"BIDDisclaimerView" owner:self options:nil] lastObject];
    _disclaimerView.frame = CGRectMake((screenSize.width-_disclaimerView.frame.size.width)/2, (screenSize.height-64-_disclaimerView.frame.size.height)/2, _disclaimerView.frame.size.width, _disclaimerView.frame.size.height);
    _disclaimerView.delegate = self;
    _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height-64)];
    [_maskView setBackgroundColor:RGBACOLOR(0, 0, 0, 0.6f)];
    //
    [self prepareData];
    [self prepareLayout];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSNumber *value = [userDefaults objectForKey:@"isAgreeDisclaimer"];
    if([value integerValue] == 1)
    {
    }
    else
    {
        [self showDisclaimerView];
    }
}

- (void)prepareData
{
    _dataSourceArr = @[@{@"title":@"爱置换", @"imgName":@"deal_exchange.png", @"className":@"BIDExchangeListViewController", @"isNeedXib":@1, @"isNeedBindingHouse":@0},
                       @{@"title":@"出租", @"imgName":@"deal_rent.png", @"className":@"BIDRentListViewController", @"isNeedXib":@0, @"isNeedBindingHouse":@0},
                       @{@"title":@"出售", @"imgName":@"deal_sale.png", @"className":@"BIDSaleHouseListViewController", @"isNeedXib":@0, @"isNeedBindingHouse":@0}];
}

- (void)prepareLayout
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [flowLayout setItemSize:CGSizeMake(screenSize.width/4, 65.f)];
    [flowLayout setMinimumLineSpacing:10];
    [flowLayout setMinimumInteritemSpacing:0];
    [flowLayout setSectionInset:UIEdgeInsetsMake(13.f, 0.f, 0.f, 0.f)];
    [_collectionView setCollectionViewLayout:flowLayout];
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
}

/**
 显示免责声明
 */
- (void)showDisclaimerView
{
    [self.view addSubview:_maskView];
    [self.view addSubview:_disclaimerView];
}

/**
 隐藏免责声明
 */
- (void)hideDisclaimerView
{
    [_disclaimerView removeFromSuperview];
    [_maskView removeFromSuperview];
}

#pragma mark - BIDDisclaimerViewDelegate
- (void)agreeDisclaimer
{
    [self hideDisclaimerView];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@1 forKey:@"isAgreeDisclaimer"];
    [userDefaults synchronize];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    BIDFunctionSubCell *functionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    NSDictionary *dic = _dataSourceArr[row];
    functionCell.imgView.image = [UIImage imageNamed:dic[@"imgName"]];
    functionCell.titleLabel.text = dic[@"title"];
    if((row+1)%4 == 0 || (_dataSourceArr.count<4 && (row+1)%(_dataSourceArr.count)==0))
    {
        functionCell.separatorLabel.hidden = YES;
    }
    else
    {
        functionCell.separatorLabel.hidden = NO;
    }
    cell = functionCell;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
    NSUInteger row = indexPath.row;
    NSDictionary *dic = _dataSourceArr[row];
    NSNumber *value = dic[@"isNeedXib"];
    BOOL isNeedXib = [value intValue]==1 ? YES : NO;
    value = dic[@"isNeedBindingHouse"];
    BOOL isNeedBindingHouse = [value intValue]==1 ? YES : NO;
    NSString *strClassName = dic[@"className"];
    //
    UIViewController *vc = nil;
    if(isNeedXib)
    {
        vc = [[NSClassFromString(strClassName) alloc] initWithNibName:strClassName bundle:nil];
    }
    else
    {
        vc = [[NSClassFromString(strClassName) alloc] init];
    }
    [self.navigationController pushViewController:vc animated:YES];
}

@end
